<?php
class ProductAction extends CommonAction {
    public function index(){
		
	
	
			
			
		if($this->_get('id')){
			$id=intval($_GET['id']);
			//当前站点数据ID
			$map['site_id']=__SITEID__;
			$map['id']=$id;
			//扩展字段
			$productModel=D('Product');
			$productExtendsModel=D('Product_extends');
			$extends=getProductExtends($id);
			$this->assign('extends',$extends);
			//规格和属性
			$skuList=$productModel->getSKUListById($id);
			$SKU=$productModel->getSKUById($id);
			//商品数据
			$info = $productModel->where(array('id'=>$id))->find(); 
			$memberInfo = $this->memberInfo; 
			
			
			if($info['levelBuy'] > 0){
				if($info['levelBuy'] > $memberInfo['level']){
					$this->error('当前等级无法查看这个商品');exit;
				}
			}
			$Cache = true;
			
			//自动上架
			if($info['starttime'] != ''&&$info['starttime']>0){
				$Cache = false;
				if(time() > $info['starttime']){
					$productModel->where(array('id'=>$id))->data(array('status' => 1))->save(); 
				}
			}
			//自动下架
			if($info['timeout'] != ''){
				$Cache = false;
				if(time() > $info['timeout'] && $info['timeout'] != 0){
					$productModel->where(array('id'=>$id))->data(array('status' => 0))->save(); 
				}
			}
			
			
			$systemagentprice = json_decode($productModel -> where(array('id' => $id)) -> getField('systemagentprice'), true);
			if($systemagentprice != ''){
				//输出支付方式
				$paymentList = M('Payment') -> where(array('status' => 1, 'site_id' => __RELATEID__)) -> select();
				$this->assign('paymentList', $paymentList);
			}
			$this->assign('systemagentprice1',$systemagentprice);
			
			
			//dump($this -> get('systemagentprice'));
			 
			$this->assign('img_list', explode(',', $info['mimg_url']));
			$imgs = explode(',', $info['mimg_url']);
			//$imgs[] = $info['img_url'];
			//主图列表
			$imgList = array();
			$imgList[0]['img_url'] = $info['img_url'];
			$imgList[0]['title'] = $info['title'];
			foreach($imgs as $k => $v){if(!empty($v)){
				$imgList[$k+1]['img_url'] = $v;
				$imgList[$k+1]['title'] = $info['title'];}
			}
			$this->assign('imgList',$imgList);
			$Product_categoryModel=D('Product_category');
			$subCategory = $Product_categoryModel -> where(array('pid' => $info['category_id'])) -> select();
			 
			    
			$curcate = $Product_categoryModel -> where(array('id' => $info['category_id'])) -> find();
			//没有子级分类,显示同级
			if(!is_array($subCategory)){
				$subCategory = $Product_categoryModel -> where(array('pid' => $curcate['pid'])) -> select();
			}
			$this->assign('subcate', $subCategory);
			$this -> assign ('curcate', $curcate);
			//页面导航
			$pagenav = $Product_categoryModel->getParentCate($info['category_id'],__SITEID__);
			$this->assign('pagenav',$pagenav);
			//模板风格
			
			
			
			//是否为砍价商品
			
			$this->iskan = M('Bargain') -> where(array('inventory' => array('gt', 0), 'product_id' => $info['id'], 'state' => 1)) -> find();
			$this->isgroup = M('Grouppurchase') -> where(array('product_id' => $info['id'], 'status' => 1)) -> find();
			
			if($this->isgroup || $this->iskan){
				$Cache = false;
				
				
				
				//当前会员参与的团
				$this -> myGrouppurchase = M('Grouppurchase_order') -> alias('g') -> join("left join __MEMBER__ as m on m.id = g.member_id") ->where(array('aff_id'=>$memberInfo['id'], 'product_id' => $info['id']))->Field("m.nickname, m.img_url,g.*") -> select();
				
				
				$aff_id = M('Grouppurchase_order')->where(array('aff_id'=>array('neq',$memberInfo['id']),'member_id'=>$memberInfo['id'],  'product_id' => $info['id']))->getField('aff_id');
				
				$this -> Grouppurchase = M('Grouppurchase_order') -> alias('g') -> join("left join __MEMBER__ as m on m.id = g.member_id") ->where(array('aff_id'=>$aff_id, 'product_id' => $info['id']))->Field("m.nickname, m.img_url,g.*") -> select();
				
				
			}
			//dump($pagenav);
			foreach($pagenav as $k=>$v){
				
				if($v['page_view_id']!=0){
					$templatec['page_view_id']=$v['page_view_id'];
				}
				if($v['page_view_header_id']!=0){
					$templatec['page_view_header_id']=$v['page_view_header_id'];
				}
				if($v['page_view_footer_id']!=0){
					$templatec['page_view_footer_id']=$v['page_view_footer_id'];
				}
			}
			
			
			
			/*foreach(array_reverse($pagenav) as $k=>$v){
				
				if($v['page_view_id']!=''){
					$templatec['page_view_id']=$v['page_view_id'];
				}
				if($v['page_view_header_id']!=''){
					$templatec['page_view_header_id']=$v['page_view_header_id'];
				}
				if($v['page_view_footer_id']!=''){
					$templatec['page_view_footer_id']=$v['page_view_footer_id'];
				}
			}*/
			//使用拼团风格显示
			if($this->isgroup){
				if($this->isgroup['page_view_id'] > 0){
					$templatec['page_view_id'] = $this->isgroup['page_view_id'];
				}
				if($this->isgroup['page_view_footer_id'] > 0){
					$templatec['page_view_footer_id'] = $this->isgroup['page_view_footer_id'];
				}
				if($this->isgroup['page_view_header_id'] > 0){
					$templatec['page_view_header_id'] = $this->isgroup['page_view_header_id'];
				}
				$this -> is_subscribe = M('Member') -> where(array('id' => $memberInfo['id'])) -> getField('is_subscribe');
				if($this -> is_subscribe == 0){
					if(C('URL_MODEL') == 2){
						 $jumpUrl = str_replace('index.php/','',__SELF__);
					}else{
						 $jumpUrl = __SELF__;
					}
					F('subscribe_returnUrl'.$memberInfo['openid'], '<a href="http://' . $_SERVER['HTTP_HOST'] .$jumpUrl.'">点击这里返回拼团</a>');
					///$this->error('请先关注公众号');exit;
				}
			}
			
			
			 
			$typeid = $Product_categoryModel->getTypeById($info['category_id'],__SITEID__);//当前类型ID
			//通过typeid查询当前类型
			$template = M('Type') -> where(array('id' => $typeid)) -> find();
			
			//关联产品 
			$this->relateids = explode(',', $info['relateids']);
			
			$relatelist = array();
			foreach($this->relateids as $k => $v){
				if(intval($v) > 0){
				    $relatelist[] = $productModel -> where(array('id' => $v, 'site_id' => __SITEID__)) -> find();
				}
			}
			$this->assign('relatelist', $relatelist);
			 
			$info['goods_id'] = $id;
			$template['template'] = $template['template']==''?'index':$template['template'];
			
			//需要购
			
			
			$isbuy = M('Sale') -> alias('s') -> join("left join __ORDER__ as o on o.id = s.order_id") -> where(array('o.status' => 2, 's.member_id' => intval($memberInfo['id']), 's.product_id' => $info['id'])) -> find();
			
			if($info['display'] == 0 && !$isbuy){
				unset($info['content']);
			}
			//不缓存
			if($info['display'] == 0){
				$Cache = false;
			}
			
			
			
			
			
			
			//dump($info['content']);
			$this->assign($info);
			$this->assign('skuList',$skuList);
			$this->assign('SKU',$SKU);
			 //dump($skuList);
			$ModulesDB = D('Modules');
		    $this->initView();
			 
			 
			 
			$seo['title']=empty($info['page_title'])?$info['title']:$info['page_title'];
			$seo['keywords']=$info['keywords'];
			$seo['description']=$info['description'];
			$this->assign('seo',$seo);
		
			 
			$this->getModulesByID($this->view, $ModulesDB);
		    $request = $this -> fetch('Public:header');
			
			$request .= $ModulesDB->getListToString($templatec['page_view_header_id'],$this->view,$Cache);
			$request .= $ModulesDB->getListToString($templatec['page_view_id'],$this->view,$Cache);
			$request .= $ModulesDB->getListToString($templatec['page_view_footer_id'],$this->view,$Cache);
			$request .= $this -> fetch('Public:footer'); 
			$request .= $this-> fetch('Public:footerscript');
			if(isset($_GET['isajaxlist'])){
				exit(json_encode(array('empty' => 0,'tbody'=>$request,'dump'=>$_REQUEST,'statu'=>1)));
			}
			//$this->display('Index:index'); 
			exit($request);
		} 
    }
	
	
	public function getAttrHtml(){
		$productModel=D('Product');
		$id = $this->_request('id');
		$info = $productModel->where(array('id'=>$id))->find(); 
		$skuList=$productModel->getSKUListById($id);
		$SKU=$productModel->getSKUById($id);
		$this->assign('skuList',$skuList);
		$this->assign('SKU',$SKU);
		$this->assign($info);
		return $this->fetch();
	}
	
	
}